package com.sis.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public abstract class DataBaseService {
	
	private String url = "jdbc:mysql://localhost:3306/"; 
	private String dbName = "seller_center";
	private String userName = "root";
	private String password = "M@gent0";
	
	private Connection connect ;
	ResultSet rs = null;
	PreparedStatement preState = null;
	
	public void getConnect() throws Exception{
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connect = DriverManager.getConnection(url+dbName,userName,password);

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}
	
	public void executeBate(String sql) throws Exception{
		
		try {
			preState = connect.prepareStatement(sql);
			preState.execute();
		} catch (SQLException e) {
			System.out.println("SQL Error :"+sql);
			e.printStackTrace();
			throw new Exception(e);
		}
	}
	
	public ResultSet executeBateRS(String sql , List<String> listArg) throws Exception{
		
		try {
			preState = connect.prepareStatement(sql);
			int index = 0;
			if(listArg != null){
				for(String arg : listArg){
					preState.setString(++index, arg);
				}
			}
			rs = preState.executeQuery();
		} catch (Exception e) {
			e.printStackTrace();
			
		   throw new Exception(e);
		}
		return rs;
	}
	
	public void closeConnect(){
		try {if(connect != null)connect.close();} catch (Exception e) {}
		try {if(preState != null)preState.close();} catch (Exception e) {}
		try {if(rs != null)rs.close();} catch (Exception e) {}
	}
	
	

}
